import _min from "lodash/min";
import _max from "lodash/max";
import _isString from "lodash/isString";
import _isEmpty from "lodash/isEmpty";
import _forEach from "lodash/forEach";

/**
 * utils
 * @date 2018/9/28

 */
import React from 'react';
import { DatePicker, Input, InputNumber, TimePicker } from 'antd';
import moment from 'moment';
import Lov from "../Lov";
import Checkbox from "../Checkbox";
import Switch from "../Switch";
import ValueList from "../ValueList";
import { DEFAULT_DATE_FORMAT, DEFAULT_TIME_FORMAT } from "../../utils/constants";
import { getDateFormat } from "../../utils/utils";
import Upload from "../Upload";
var TextArea = Input.TextArea;
/**
 * dealFieldProps - 处理基本的属性
 * @param {Object} field - 字段
 */

function dealFieldProps() {
  var field = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  var props = {};

  _forEach(field.props, function (prop) {
    props[prop.attributeName] = prop.attributeValue;
  });

  return props;
}
/**
 * getInputNumberElement - 获取 InputNumber 的 render
 * @param {Object} field - 字段
 */


function getInputNumberElement(_ref) {
  var field = _ref.field;
  var props = dealFieldProps(field);
  return /*#__PURE__*/React.createElement(InputNumber, props);
}
/**
 * getCheckboxElement - 获取 Checkbox 的 render
 * @param {Object} field - 字段
 */


function getCheckboxElement(_ref2) {
  var field = _ref2.field;
  var props = dealFieldProps(field);
  return /*#__PURE__*/React.createElement(Checkbox, props);
}
/**
 * getSwitchElement - 获取 Switch 的 render
 * @param {Object} field - 字段
 */


function getSwitchElement(_ref3) {
  var field = _ref3.field;
  var props = dealFieldProps(field);
  return /*#__PURE__*/React.createElement(Switch, props);
}
/**
 * getLovElement - 获取 Lov 的 render
 * @param {Object} field - 字段
 */


function getLovElement(_ref4) {
  var field = _ref4.field,
      organizationId = _ref4.organizationId,
      _ref4$dataSource = _ref4.dataSource,
      dataSource = _ref4$dataSource === void 0 ? {} : _ref4$dataSource;

  if (_isEmpty(field.lovCode)) {
    return getInputElement({
      field: field
    });
  } else {
    var props = dealFieldProps(field);
    return /*#__PURE__*/React.createElement(Lov, Object.assign({}, props, {
      code: field.lovCode,
      queryParams: {
        organizationId: organizationId,
        tenantId: organizationId
      },
      textValue: dataSource[getDisplayFieldCode(field)]
    }));
  }
}
/**
 * getValueListElement - 获取 ValueList 的 render
 * @param {Object} field - 字段
 */


function getValueListElement(_ref5) {
  var field = _ref5.field,
      organizationId = _ref5.organizationId,
      _ref5$dataSource = _ref5.dataSource,
      dataSource = _ref5$dataSource === void 0 ? {} : _ref5$dataSource;

  if (_isEmpty(field.lovCode)) {
    return getInputElement({
      field: field
    });
  } else {
    var props = dealFieldProps(field);
    return /*#__PURE__*/React.createElement(ValueList, Object.assign({}, props, {
      lovCode: field.lovCode,
      organizationId: organizationId,
      textField: dataSource[getDisplayFieldCode(field)],
      textValue: dataSource[getDisplayFieldCode(field)]
    }));
  }
}
/**
 * getUploadElement - 获取 Upload 的 render
 * @param {Object} field - 字段
 */


function getUploadElement(_ref6) {
  var field = _ref6.field,
      organizationId = _ref6.organizationId;

  if (_isEmpty(field.lovCode)) {
    return getInputElement({
      field: field
    });
  } else {
    var props = dealFieldProps(field);
    return /*#__PURE__*/React.createElement(Upload, Object.assign({}, props, {
      organizationId: organizationId
    }));
  }
}
/**
 * getInputElement - 获取 Input 的 render
 * @param {Object} field - 字段
 */


function getInputElement(_ref7) {
  var field = _ref7.field;
  var props = dealFieldProps(field);
  return /*#__PURE__*/React.createElement(Input, props);
}
/**
 * getDatePickerElement - 获取 DatePicker 的 render
 * @param {Object} field - 字段
 */


function getDatePickerElement(_ref8) {
  var field = _ref8.field;
  var props = dealFieldProps(field);
  return /*#__PURE__*/React.createElement(DatePicker, props);
}
/**
 * getDateTimePickerElement - 获取 TimePicker 的 render
 * @param {Object} field - 字段
 */


function getDateTimePickerElement(_ref9) {
  var field = _ref9.field;
  var props = dealFieldProps(field);
  return /*#__PURE__*/React.createElement(TimePicker, props);
}
/**
 * getTextAreaElement - 获取 TextArea 的 render
 * @param {Object} field - 字段
 */


function getTextAreaElement(_ref10) {
  var field = _ref10.field;
  var props = dealFieldProps(field);
  return /*#__PURE__*/React.createElement(TextArea, props);
}
/**
 * @param {!Object} config - 配置
 * @param {Object} config.field - 字段
 * @param {Number} config.organizationId - 租户id
 * @param {Object} config.dataSource - 表单值
 */


export function getFieldElement() {
  var config = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};

  switch (config.field.componentType) {
    case 'InputNumber':
      return getInputNumberElement(config);

    case 'TextArea':
      return getTextAreaElement(config);

    case 'DatePicker':
      return getDatePickerElement(config);

    case 'DateTimePicker':
      return getDateTimePickerElement(config);

    case 'Switch':
      return getSwitchElement(config);

    case 'Checkbox':
      return getCheckboxElement(config);

    case 'Lov':
      return getLovElement(config);

    case 'ValueList':
      return getValueListElement(config);

    case 'Upload':
      return getUploadElement(config);

    case 'Input':
    default:
      return getInputElement(config);
  }
}
/**
 * getWidthFromWord - 通过字符串确定宽度
 * @param {String} word - 字符串
 * @param {Number} minWidth - 最小宽度
 * @param {Number} maxWidth - 最大宽度
 * @param {Number} [defaultWidth=100] - 默认宽度
 * @param {Number} [fontWidth=12] - 每个字符的宽度
 * @param {Number} [paddingWidth=36] - 补偿额外宽度
 */

export function getWidthFromWord(_ref11) {
  var word = _ref11.word,
      minWidth = _ref11.minWidth,
      maxWidth = _ref11.maxWidth,
      _ref11$defaultWidth = _ref11.defaultWidth,
      defaultWidth = _ref11$defaultWidth === void 0 ? 100 : _ref11$defaultWidth,
      _ref11$fontWidth = _ref11.fontWidth,
      fontWidth = _ref11$fontWidth === void 0 ? 12 : _ref11$fontWidth,
      _ref11$paddingWidth = _ref11.paddingWidth,
      paddingWidth = _ref11$paddingWidth === void 0 ? 36 : _ref11$paddingWidth;
  var ret = defaultWidth;

  if (_isString(word)) {
    ret = word.length * fontWidth;

    if (_min) {
      ret = _max([ret, minWidth]);
    }

    if (_max) {
      ret = _min([ret, maxWidth]);
    }

    ret += paddingWidth;
  }

  return ret;
}
/**
 * 获取属性中的 displayCode 没有这个属性的话, 会返回空字符串
 * @param props
 * @returns {string}
 */

function getDisplayFieldCodeAttrValue(props) {
  var displayFieldCode = '';

  _forEach(props, function (prop) {
    if (prop.attributeName === 'displayFieldCode') {
      displayFieldCode = props.attributeValue;
      return false;
    }
  });

  return displayFieldCode;
}
/**
 * 获取属性上的 format, 如果没有, 返回 defaultFormat
 * @param field
 * @param defaultFormat
 * @returns {*}
 */


function getDatePickerFormat(field, defaultFormat) {
  var format = defaultFormat;

  _forEach(field.props, function (prop) {
    if (prop.attributeName === 'format') {
      format = prop.attributeValue;
      return false;
    }
  });

  return format;
}
/**
 * 通过 field 和 dataSource 获取显示值
 * @param {Object} field
 * @param {Object} dataSource
 * @returns
 */


export function getDisplayValue(field, dataSource) {
  var displayFieldCode = getDisplayFieldCodeAttrValue(field.props);
  var format;
  var dateStr;

  switch (field.componentType) {
    case 'ValueList':
    case 'Lov':
      return dataSource[displayFieldCode] || dataSource["".concat(field.fieldCode, "Meaning")] || dataSource[field.fieldCode];

    case 'DatePicker':
      format = getDatePickerFormat(field, DEFAULT_DATE_FORMAT);
      dateStr = dataSource[displayFieldCode] || dataSource[field.fieldCode];
      return dateStr && moment(dateStr, format).format(getDateFormat());

    case 'TimePicker':
      format = getDatePickerFormat(field, DEFAULT_TIME_FORMAT);
      dateStr = dataSource[displayFieldCode] || dataSource[field.fieldCode];
      return dateStr && moment(dateStr, format).format(format);
    // Upload 单独自己处理
    // case 'Upload':
    //   return (
    //     dataSource[field.fieldCode] && (
    //       <Upload viewOnly attachmentUUID={dataSource[field.fieldCode]} />
    //     )
    //   );

    default:
      return dataSource[displayFieldCode] || dataSource[field.fieldCode];
  }
}
/**
 * 获取用来显示的字段
 * @param field
 * @returns {*}
 */

export function getDisplayFieldCode(field) {
  var displayFieldCode = getDisplayFieldCodeAttrValue(field.props);

  if (displayFieldCode) {
    return displayFieldCode;
  }

  switch (field.componentType) {
    case 'ValueList':
      return "".concat(field.fieldCode, "Meaning");

    case 'Lov':
      return "".concat(field.fieldCode, "Meaning");

    default:
      return field.fieldCode;
  }
}